package com.wl.api.controller.system;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.wl.api.common.utils.BeanUtils;
import com.wl.api.common.utils.JsonObject;
import com.wl.api.common.utils.StringUtil;
import com.wl.api.controller.base.CommonController;
import com.wl.api.dto.business.CustomerProductDTO;
import com.wl.api.model.business.CustomerProduct;
import com.wl.api.service.business.ICustomerProductService;

@RestController
@RequestMapping("/admin/customer/product")
public class AdminCustomerProductController extends CommonController {

	@Autowired
	ICustomerProductService customerProductSerivce;
	
	@RequestMapping(value = "/manage", method=RequestMethod.POST)
	public JsonObject insert(@RequestBody CustomerProductDTO param) {
		JsonObject json = JsonObject.getInstance();
		boolean pass = true;
		String title = "";
		if (param.getProductId()!=null || StringUtil.isEmpty(param.getProductNameCn()) || StringUtil.isEmail(param.getProductNameEn())) {
			title = message_operation_fail;
			pass = false;
		}
		if (pass) {
			customerProductSerivce.insert(BeanUtils.copyProperties(param, CustomerProduct.class));
		}
		setJsonMessage(json, title, pass);
		return json;
	}
	

	@RequestMapping(value = "/manage", method=RequestMethod.PUT)
	public JsonObject update(@RequestBody CustomerProductDTO param) {
		JsonObject json = JsonObject.getInstance();
		boolean pass = true;
		String title = "";
		if (param.getProductId()==null || StringUtil.isEmpty(param.getProductNameCn()) || StringUtil.isEmail(param.getProductNameEn())) {
			title = message_operation_fail;
			pass = false;
		}
		if (pass) {
			customerProductSerivce.insert(BeanUtils.copyProperties(param, CustomerProduct.class));
		}
		setJsonMessage(json, title, pass);
		return json;
	}
	
	@RequestMapping(value = "/manage/{id}", method=RequestMethod.DELETE)
	public JsonObject update(@PathVariable("id") Long id) {
		JsonObject json = JsonObject.getInstance();
		boolean pass = true;
		String title = "";
		customerProductSerivce.updateById(CustomerProduct.builder().isDeleted(1).createDate(new Date()).productId(id).build());
		setJsonMessage(json, title, pass);
		return json;
	}
	
	
	@RequestMapping(value="/manage", method= RequestMethod.GET)
	public JsonObject getProductList() {
		JsonObject json = JsonObject.getInstance();
		List<CustomerProduct> list = customerProductSerivce.selectList(new EntityWrapper<>(CustomerProduct.builder().isDeleted(0).build()));
		json.setData(list);
		setJsonMessage(json);
		return json;
	}
	
	
}
